/**
 * 
 */
package com.eagle.coders.core.action.delegates;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;

import com.eagle.coders.common.form.annotations.type.ActionPerformerType;
import com.eagle.coders.common.form.interfaces.ExecutionRequest;
import com.eagle.coders.common.form.interfaces.ExecutionResponse;
import com.eagle.coders.common.form.interfaces.IExecution;
import com.eagle.coders.core.action.dao.ActionDao;
import com.eagle.coders.core.action.dao.annotation.ActionProcessor;
import com.eagle.coders.core.exceptions.BaseAppException;
import com.eagle.coders.rules.service.DroolsRuleService;
import com.eagle.coders.rules.service.RuleService;
import com.eagle.coders.xml.transformation.beans.Action;

/**
 * @author Anees
 *
 */
public class ActionsDelegate {
	@Inject @ActionProcessor ActionDao actionDao;

	public ActionResponse executeAction(ActionRequest actionRequest){
		ActionResponse actionResponse = new ActionResponse();
		Map<String, Map<String, Object>> beanValueMap = actionRequest.getBeanValueModelMap();
		actionRequest.getDomainBeanValueMap();
		Action action = actionRequest.getAction();
		
		
		if(action.getPerformerType().equals(ActionPerformerType.JAVA.toString())){
			String performer = action.getActionPerformer();
			try{
				IExecution execution =(IExecution) Class.forName(performer).newInstance();
				ExecutionRequest request = new ExecutionRequest();
				
				List<Object> list = new ArrayList<Object>();
				if(null != beanValueMap && beanValueMap.size() > 0){
					list.add(beanValueMap);
				} else
					list.add(actionRequest.getDomainBeanValueMap());
				
				request.setObjectsToProceed(list);
				request.setUsecaseId(actionRequest.getUsecaseId());
				ExecutionResponse response = execution.execute(request);
				if(null != response.getErrors() && response.getErrors().size() > 0){
					actionResponse.setErrors(response.getErrors());
//					throw new BaseAppException("Action Got Error/Exception", response.getErrors());
				}else{
					System.out.println("[AcionsDelegate] :: response :: objects "+ response.getObjectList());
					List<? extends Object> objectList = response.getObjectList();
					actionResponse.setResonseObjectList(objectList);
					actionResponse.setSearchTableGeneratorMap(response.getSearchTableGeneratorMap());
				}
				actionResponse.setActionType(response.getActionTypes());
			}
			catch(BaseAppException e){
//				e.printStackTrace();
				System.out.println("ActionDelegates :: Error Happend" );
				Map<String, String> errors= e.getBaseAppErrorMessageMap();
//				Set<String> keys= errors.keySet();
//				for(String key : keys){
//					actionResponse.addError(key, errors.get(key));
//				}
				actionResponse.addError("BaseAppException", e.getMessage());
			}
			catch(ClassNotFoundException cne){
				actionResponse.addError("ExecutionNotFound", cne.getMessage());
			}catch(IllegalAccessException ele){
				actionResponse.addError("SecurityError", ele.getMessage());
			}catch(InstantiationException ie){
				actionResponse.addError("NotValidClass", ie.getMessage());
			}catch(Exception e){
				System.out.println("ActionDelegates :: Error Happend;;;;; FUCK" );
				e.printStackTrace();
				actionResponse.addError("BaseError", e.getMessage());
			}
		}
		
		return actionResponse;
	}
	
	/**
	 * 
	 * @param action
	 */
	private void executeRule(Action action, Map<String, Map<String, Object>> beanValueMap){
		System.out.println("[ActionDelegate]{executeRule}");
//		try{
//			
//			Context ctx = new InitialContext();
//			SecurityService bean =(SecurityService) ctx.lookup("eagle/SecurityServiceBean/remote");
////			bean.executeServiceByRule(beanValueMap, action.getPerformerPathID());
//		}catch(Exception e){
//			e.printStackTrace();
//		}

//		SecurityDao securityDao = new SecurityDaoImpl();
//		securityDao.execute(beanValueMap, action.getPerformerPathID());
//		securityDao.authenticateUser(usersBean)
	}
}
